/* Copyright (C) 2011 clueless <clueless.mmopeon@gmail.com>
 *
 * This file is part of MMO Peon.
 *
 * MMO Peon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MMO Peon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MMO Peon.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <Windows.h>
#include "harvest.h"
#include "../log.h"
#include "../classes/objectmap.h"
#include "../commontypes.h"
#include "../packets.h"

HarvestState::HarvestState(PlayerMovement *movement) : BaseState(10)
{
    this->movement = movement;
    this->wps = wps;
    sentPacket = false;
    gotFlag = false;
}

bool HarvestState::Ready()
{
    ObjectMap objects(true);
    ObjectMap wood = objects.Filter(560018).Filter(190.0, memBase->player->coords);

    if (wood.Count() > 0 && memBase != 0 && memBase->player != 0)
        return true;
    else
        return false;
}

void HarvestState::Update()
{
    ObjectMap objects(true);
    ObjectMap wood = objects.Filter(560018).Filter(190.0, memBase->player->coords);

    if (wood.Count() <= 0)
        return;

    if (!movement->IsBusy() && wood.Begin()->second.coords.Distance(memBase->player->coords) > 20.0)
    {
        movement->Move(wood.Begin()->second.coords);
    }
    else if (!movement->IsBusy() && wood.Begin()->second.coords.Distance(memBase->player->coords) < 20.0)
    {
        // We are near a harvesting node
        if (!sentPacket)
        {
            // We have not yet sent a packet
            LogWrite(LOG_NORMAL, "Sending harvest packet");
            sentPacket = true;
            SendHarvestPacket(wood.Begin()->second.objectId);
        }
        else if (sentPacket && gotFlag && memBase->player->harvesting == -1)
        {
            // We sent a packet, we were harvesting at some point but not anymore
            LogWrite(LOG_NORMAL, "Not Harvesting anymore");
            sentPacket = false;
            gotFlag = false;
        }
        else if (sentPacket && !gotFlag && memBase->player->harvesting != -1)
        {
            LogWrite(LOG_NORMAL, "Sent packet, harvesting now");
            // We sent the packet, are harvesting now, but werent before
            gotFlag = true;
        }
    }
    movement->Update();
}

void HarvestState::Exit()
{
    sentPacket = false;
    gotFlag = false;
    movement->Stop();
    movement->SetFlags(PlayerMovement::Flags::Forward); // make sure we stop 'casting' harvest
}

void HarvestState::SendHarvestPacket(uint32_t objectId)
{
    uint32_t packet[] = {0x640, objectId};
    SendPacket(8, (uint8_t *)packet);
}
